package org.sippr.rvt2ifc.service.impl;

import lombok.Getter;
import org.sippr.rvt2ifc.dao.ProjectDao;
import org.sippr.rvt2ifc.dao.ProjectModelRelationDao;
import org.sippr.rvt2ifc.dao.UploadedFileDao;
import org.sippr.rvt2ifc.dao.XktModelDao;
import org.sippr.rvt2ifc.pojo.Project;
import org.sippr.rvt2ifc.pojo.ProjectModelRelation;
import org.sippr.rvt2ifc.pojo.UploadedFile;
import org.sippr.rvt2ifc.pojo.XktModel;
import org.sippr.rvt2ifc.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private XktModelDao xktModelDao;
    @Autowired
    private UploadedFileDao uploadedFileDao;
    @Autowired
    private ProjectModelRelationDao projectModelRelationDao;
    @Getter
    public static String BASE_URL;

    @Value("${bim.viewer.url}")
    public void setUrl(String url) {
        BASE_URL = url;
    }

    @Override
    public int insert(Project record) {
        return projectDao.insert(record);
    }

    @Override
    public Project selectByUuid(String uuid) {
        Project project = projectDao.selectByUuid(uuid);
        getOtherData(project);
        return project;
    }

    @Override
    public List<Project> list() {
        List<Project> list = projectDao.selectAll();
        for (Project p : list) {
            getOtherData(p);
        }
        return list;
    }

    private void getOtherData(Project project) {
        List<ProjectModelRelation> projectModelRelationList_1 = projectModelRelationDao.selectByProjectUuid(project.getUuid());
        List<String> modelUuids = new ArrayList<>();
        List<String> modelNames = new ArrayList<>();
        for (ProjectModelRelation pmr : projectModelRelationList_1) {
            modelUuids.add(pmr.getXktModelUuid());
            XktModel xktModel = xktModelDao.selectByUuid(pmr.getXktModelUuid());
            UploadedFile uploadedFile = uploadedFileDao.selectByUuid(xktModel.getUploadedFileUuid());
            modelNames.add(uploadedFile.getFileName());
        }
        project.setUrl(BASE_URL + "?projectId=" + project.getUuid() + "&time=" + System.currentTimeMillis());
        project.setModelUuids(modelUuids);
        project.setModelNames(modelNames);
    }

    @Override
    public List<Project> getWaitProject() {
        List<Project> list = projectDao.selectWaitProject();

        for (Project p : list) {
            List<ProjectModelRelation> projectModelRelationList_1 = projectModelRelationDao.selectByProjectUuid(p.getUuid());
            List<String> lonelyProjectUuids = new ArrayList<>();
            List<String> modelUuids = new ArrayList<>();
            List<String> modelNames = new ArrayList<>();
            for (ProjectModelRelation pmr : projectModelRelationList_1) {
                Project project = getLonelyProject(pmr.getXktModelUuid());
                if (project == null) {
                    break;
                }
                lonelyProjectUuids.add(project.getUuid());
                modelUuids.add(pmr.getXktModelUuid());
                XktModel xktModel = xktModelDao.selectByUuid(pmr.getXktModelUuid());
                UploadedFile uploadedFile = uploadedFileDao.selectByUuid(xktModel.getUploadedFileUuid());
                modelNames.add(uploadedFile.getPinyinName());
            }
            p.setLonelyProjectUuids(lonelyProjectUuids);
            p.setModelUuids(modelUuids);
            p.setModelNames(modelNames);
        }
        return list;
    }

    @Override
    public void updateState(String projectUuid, Short state) {
        Project project = projectDao.selectByUuid(projectUuid);
        if (project == null) {
            return;
        }
        project.setState(state);
        projectDao.updateByPrimaryKey(project);
    }

    @Override
    public void delete(List<String> uuids) {

        for (String uuid : uuids) {
            projectDao.deleteByUuid(uuid);
            projectModelRelationDao.deleteByProjectUuid(uuid);
        }

    }

    private Project getLonelyProject(String modelUuid) {
        List<ProjectModelRelation> projectModelRelationList = projectModelRelationDao.selectByModelUuid(modelUuid);
        for (ProjectModelRelation projectModelRelation : projectModelRelationList) {
            Project project = projectDao.selectByUuidAndState(projectModelRelation.getProjectUuid(), 3);
            if (project != null)
                return project;
        }
        return null;
    }
}
